package com.zb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.Service.NoticeService;
import com.zb.Service.RecordService;
import com.zb.Service.UserService;
import com.zb.entity.Notice;
import com.zb.entity.User;
import com.zb.util.PageUtil;

@Scope("prototype")
@Component
@RequestMapping("/notice")
public class NoticeController {
	@Resource
	private NoticeService ns;
	@Resource
	private UserService us;
	@Resource
	private RecordService rs;
	@RequestMapping(params = "op=update")
	public String toUpdate(String tital, String content, String startshow,
			String endshow,Integer noticelv,Integer state,Integer id) {
		Notice n = ns.getNoticeById(id);
		n.setTital(tital);
		n.setContent(content);
		n.setStartshow(startshow);
		n.setEndshow(endshow);
		n.setNoticelv(noticelv);
		n.setState(state);
		ns.updateNotice(n);
		return "noticAll";
	}

	@RequestMapping(params = "op=show4")
	@ResponseBody
	private Map<String, Object> toshow4(Integer page, Integer rows, int id) {
		PageUtil<Notice> pageUtil = new PageUtil<Notice>();
		pageUtil.setIndex(page);
		pageUtil.setSize(rows);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Notice> nlist = ns.findNoticeAllNoByUserId(pageUtil.getIndex(),
				pageUtil.getSize(), id);
		pageUtil.setData(nlist);
		int count = ns.findNoticeAllNoByUserIdcount(id);
		pageUtil.setCount(count);
		map.put("total", pageUtil.getCount());
		map.put("rows", pageUtil.getData());
		return map;
	}

	@RequestMapping(params = "op=show2")
	@ResponseBody
	private Map<String, Object> toshow(Integer page, Integer rows) {
		PageUtil<Notice> pageUtil = new PageUtil<Notice>();
		pageUtil.setIndex(page);
		pageUtil.setSize(rows);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Notice> nlist = ns.findNoticeAllOk(pageUtil.getIndex(), pageUtil
				.getSize());
		pageUtil.setData(nlist);
		int count = ns.findNoticeAllOkcount();
		pageUtil.setCount(count);
		map.put("total", pageUtil.getCount());
		map.put("rows", pageUtil.getData());
		return map;
	}

	@RequestMapping(params = "op=show3")
	private String toshow3(int id, ModelMap modelMap) {
		Notice n = ns.getNoticeById(id);
		modelMap.put("n", n);
		return "notic";
	}

	@RequestMapping(params = "op=show5")
	public String tosave(int id, String tital, String content,
			String startshow, String endshow, Integer state, Integer noticelv,HttpSession session) {
		Notice n = new Notice();
		User u = us.getUserById(id);
		n.setTital(tital);
		n.setContent(content);
		n.setStartshow(startshow);
		n.setEndshow(endshow);
		n.setState(state);
		n.setNoticelv(noticelv);
		n.setUser(u);

		ns.saveNotice(n);
		User user = (User) session.getAttribute("sys_user");
		rs.addRecord(user.getId(), "发布公告");
		return "noticAll";
	}

	@RequestMapping(params = "op=show7")
	@ResponseBody
	private List<Notice> toshow7() {
		List<Notice> nlist = ns.findNoticeAllOk(1, 5);
		return nlist;
	}

}
